package com.itheima.edu.info.manger.contrller;

import com.itheima.edu.info.manger.domain.Student;
import com.itheima.edu.info.manger.service.StudentService;

import java.util.Scanner;

public class StudentContrller {
    private StudentService service=new StudentService();
    private Scanner sc=new Scanner(System.in);
    public  void start() {
       studentloop: while (true) {
        System.out.println("---------------------欢迎来到学生管理系统----------------------");
        System.out.println("请输入你的选择：1.添加学生信息 2.删除学生信息  3.更新学生信息  4.查询学生信息 5.退出");
        Scanner sc=new Scanner(System.in);
         String chiose=sc.next();
             switch (chiose) {
                 case "1":
                     addstudent();
                     break;
                 case "2":
                     deleteid();
                     break;
                 case "3":
                     updatestudent();
                     break;
                 case "4":
                     findstudent();
                     break;
                 case "5":
                     System.out.println("退出系统");
                     break studentloop;
                 default:
                     System.out.println("输入错误");
             }
         }
    }
    public  void addstudent() {
        //1.键盘获取录入的信息

        String id;
        while (true){
            System.out.println("请输入学生id");
             id=sc.next();
           boolean flog= service.isExcies(id);
           if(flog){
               System.out.println("输入学号冲突");
           }else {
               break;
           }
        }
        Student stu= studentinfo(id);

        //4.将学生对象stu传递到service的addstudent中进行业务处理,
         boolean result= service.adddstudent(stu);
        //5.控制台显示添加成功或者失败
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    public  void deleteid() {
       String deid =inputstudent();

        //调用service方法的deleteid，根据id删除学生
        service.deleteid(deid);
        //提示删除成功
        System.out.println("删除成功");
    }
    public  void updatestudent() {

        String upid= inputstudent();

        Student sutnew=studentinfo(upid);
         service.updatastudent(upid,sutnew);
        System.out.println("修改成功");
    }
    public  void findstudent() {
      //1.调用service方法中findstudent方法，得到学生数组
        Student[] stus=service.findstudent();
//2.判断数组地址是否为空
        if(stus==null){
            System.out.println("没有此信息,重新添加");
            return;
        }
        //遍历数组，打印到控制台
        System.out.println("学号\t姓名\t年龄\t地址\t");
        for (int i = 0; i < stus.length; i++) {
        Student stus1=stus[i];
        if(stus1 !=null){
            System.out.println(stus1.getId()+"\t\t"+stus1.getName()+"\t\t"+stus1.getAge()+"\t\t"+stus1.getAddress());
        }

        }
    }
    public  String inputstudent(){
        String id;
        while (true) {
            //键盘录入要删除的学生id
            System.out.println("请输入您的id:");
            id = sc.next();
            //判断id是否存在
            boolean excies = service.isExcies(id);
            if (!excies) {
                System.out.println("输入不存在,重新书输入：");
            } else {
                break;
            }
        }
            return id;

}
    public Student studentinfo(String id){
        System.out.println("请输入学生姓名");
        String name=sc.next();
        System.out.println("请输入学生年龄");
        String age=sc.next();
        System.out.println("请输入学生地址");
        String address=sc.next();
        //2.将获取的信息封装为学生对象
        Student stu=new Student();
        //3.封装对象
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setAddress(address);
        return stu;
    }
}